﻿/*
 * -------------------------------------------------------------------------------
 * 版权信息：	
 * 功能描述：	代码处成器生成
 * 
 * 创建人：　	代码生成
 * 创建日期：	2021-04-21 12:40:50
 * 创建说明：	
 * 
 * 修改人：　	
 * 修改日期：	
 * 修改说明：	
 *    
 * -------------------------------------------------------------------------------
 */

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using MS.Entities;
using MS.Enums;
using MS.Utility;
using MS.Utility.EF;
using MS.Enums.Common;
using System.Data.Common;
using MS.Utility.Logging;

namespace MS.DataAccess
{
    public partial class MS_UserCenter_UserAttrDataAccess
    {

        /// <summary>
        /// 用户属性表-增加方法
        /// </summary>
        public void Add(MS_UserCenter_UserAttr ms_UserCenter_UserAttr)
        {
            using (MSDbContext context = new MSDbContext(WriteRead.Write))
            {
               context.MS_UserCenter_UserAttr.Add(ms_UserCenter_UserAttr);
               context.SaveChanges();
            }
			LogHelper.AddOperate("Uid = " + ms_UserCenter_UserAttr.Uid , ms_UserCenter_UserAttr);
        }
	
        /// <summary>
        /// 用户属性表-删除方法
        /// </summary>
        public void Remove(long uid)
        {
			MS_UserCenter_UserAttr ms_UserCenter_UserAttr = new MS_UserCenter_UserAttr();
            using (MSDbContext context = new MSDbContext(WriteRead.Write))
            {
                 ms_UserCenter_UserAttr = context.MS_UserCenter_UserAttr.Find(uid);
                 context.MS_UserCenter_UserAttr.Remove(ms_UserCenter_UserAttr);
                 context.SaveChanges();
            }
			LogHelper.DeleteOperate("Uid = " + ms_UserCenter_UserAttr.Uid , ms_UserCenter_UserAttr);
        }

        /// <summary>
        /// 用户属性表-删除方法
        /// </summary>
        public void Remove(Expression<Func<MS_UserCenter_UserAttr, bool>> condition)
        {
			List<MS_UserCenter_UserAttr> ms_UserCenter_UserAttrs = null;
            using (MSDbContext context = new MSDbContext(WriteRead.Write))
            {
                 ms_UserCenter_UserAttrs = context.MS_UserCenter_UserAttr.Where(condition).ToList();
                    context.MS_UserCenter_UserAttr.RemoveRange(ms_UserCenter_UserAttrs);
                 context.SaveChanges();
            }

			if (ms_UserCenter_UserAttrs != null)
            {
                foreach (var ms_UserCenter_UserAttr in ms_UserCenter_UserAttrs)
                {
                    LogHelper.DeleteOperate("Uid = " + ms_UserCenter_UserAttr.Uid , ms_UserCenter_UserAttr);
                }
            }
        }

        /// <summary>
        /// 用户属性表-修改方法
        /// </summary>
        public void Save(MS_UserCenter_UserAttr ms_UserCenter_UserAttr)
        {
            using (MSDbContext context = new MSDbContext(WriteRead.Write))
            {
                 context.Entry(ms_UserCenter_UserAttr).State = EntityState.Modified;
                 context.SaveChanges();
            }
			LogHelper.UpdateOperate("Uid = " + ms_UserCenter_UserAttr.Uid , null, ms_UserCenter_UserAttr);
        }

        /// <summary>
        /// 用户属性表-根据主键获取实体信息
        /// </summary>
        /// <returns></returns>
        public MS_UserCenter_UserAttr GetById(long uid)
        {
            using (MSDbContext context = new MSDbContext(WriteRead.Read))
            {
                return context.MS_UserCenter_UserAttr.Find(uid);
            }
        }

		
        /// <summary>
        /// 用户属性表-根据查询条件获取实体信息
        /// <param name="condition">查询条件</param>
        /// </summary>
        /// <returns></returns>
        public MS_UserCenter_UserAttr GetItemByQuery(Expression<Func<MS_UserCenter_UserAttr, bool>> condition)
        {
            using (MSDbContext context = new MSDbContext(WriteRead.Read))
            {
                return context.MS_UserCenter_UserAttr.SingleOrDefault(condition);
            }
        }

        /// <summary>
        /// 用户属性表-根据条件查询是否存在
        /// <param name="condition">查询条件</param>
        /// </summary>
        /// <returns></returns>
        public bool IsExists(Expression<Func<MS_UserCenter_UserAttr, bool>> condition)
        {
            using (MSDbContext context = new MSDbContext(WriteRead.Read))
            {
                return context.MS_UserCenter_UserAttr.Any(condition);
            }
        }

		/// <summary>
        /// 用户属性表-根据查询条件获取总数
        /// <param name="condition">查询条件</param>
        /// </summary>
        /// <returns></returns>
        public int GetCountByQuery(Expression<Func<MS_UserCenter_UserAttr, bool>> condition)
        {
		    using (MSDbContext context = new MSDbContext(WriteRead.Read))
            {
                return context.MS_UserCenter_UserAttr.Where(condition).Count();
            }
		}

        /// <summary>
        /// 用户属性表-获取所有
        /// </summary>
        /// <returns></returns>
        public List<MS_UserCenter_UserAttr> GetAll()
        {
            List<MS_UserCenter_UserAttr> ms_UserCenter_UserAttrs = new List<MS_UserCenter_UserAttr>();
            using (MSDbContext context = new MSDbContext(WriteRead.Read))
            {
                ms_UserCenter_UserAttrs = context.MS_UserCenter_UserAttr.ToList();
            }
            return ms_UserCenter_UserAttrs;
        }

        /// <summary>
        /// 用户属性表-分页查询
        /// </summary>
        /// <param name="startRowIndex">索引开始</param>
        /// <param name="maximumRows">每页信息条数</param>
        /// <param name="condition">查询条件</param>
        /// <param name="totalRecords">总计录数</param>
		/// <param name="orderBy">排序条件</param>
        /// <param name="isDesc">是否倒序</param>
        /// <returns></returns>
		public List<MS_UserCenter_UserAttr> GetPagedList(int startRowIndex, int maximumRows, Expression<Func<MS_UserCenter_UserAttr,bool>> condition, string orderBy, bool isDesc, out int totalRecords)
        {
            List<MS_UserCenter_UserAttr> ms_UserCenter_UserAttrs = new List<MS_UserCenter_UserAttr>();
            using (MSDbContext context = new MSDbContext(WriteRead.Read))
            {
				var _ms_UserCenter_UserAttrs = context.MS_UserCenter_UserAttr.Where<MS_UserCenter_UserAttr>(condition);
                if (!string.IsNullOrEmpty(orderBy))
                {
                    try
                        {
                            string[] split = orderBy.Split(',');
                            for (int i = 0; i < split.Length; i++)
                            {
                                string[] param = split[i].Split(' ');
                                if(param.Length > 1)
                                {
                                    if (i == 0)
                                    {
                                        _ms_UserCenter_UserAttrs = _ms_UserCenter_UserAttrs.OrderBy(param[0], param[1] == "desc");
                                    }
                                    else
                                    {
                                        _ms_UserCenter_UserAttrs = _ms_UserCenter_UserAttrs.ThenBy(param[0], param[1] == "desc");
                                    }
                                }
                                else
                                {
                                    if (i == 0)
                                    {
                                        _ms_UserCenter_UserAttrs = _ms_UserCenter_UserAttrs.OrderBy(param[0], false);
                                    }
                                    else
                                    {
                                        _ms_UserCenter_UserAttrs = _ms_UserCenter_UserAttrs.ThenBy(param[0], false);
                                    }
                                }
                            }
                        }
                        catch
                        {
                            throw new Exception("OrderBy参数错误");
                        }
                }
                ms_UserCenter_UserAttrs = _ms_UserCenter_UserAttrs.Skip(startRowIndex).Take(maximumRows).ToList();
                totalRecords = context.MS_UserCenter_UserAttr.Where<MS_UserCenter_UserAttr>(condition).Count();
            }
            return ms_UserCenter_UserAttrs;
        }

		/// <summary>
        /// 用户属性表-分页查询
        /// </summary>
        /// <param name="startRowIndex">索引开始</param>
        /// <param name="maximumRows">每页信息条数</param>
        /// <param name="condition">查询条件</param>
        /// <param name="totalRecords">总计录数</param>
		/// <param name="orderBy">排序条件</param>
        /// <param name="isDesc">是否倒序</param>
        /// <returns></returns>
		public List<MS_UserCenter_UserAttr> GetPagedListExpression(int startRowIndex, int maximumRows, Expression<Func<MS_UserCenter_UserAttr,bool>> condition, Expression<Func<MS_UserCenter_UserAttr, object>> orderBy, bool isDesc, out int totalRecords)
        {
            List<MS_UserCenter_UserAttr> ms_UserCenter_UserAttrs = new List<MS_UserCenter_UserAttr>();
            using (MSDbContext context = new MSDbContext(WriteRead.Read))
            {
				var _ms_UserCenter_UserAttrs = context.MS_UserCenter_UserAttr.Where<MS_UserCenter_UserAttr>(condition);
                if (orderBy != null)
                {
                    _ms_UserCenter_UserAttrs = _ms_UserCenter_UserAttrs.OrderBy(EFHelper.GetPropertyName<MS_UserCenter_UserAttr>(orderBy), isDesc);
                }
				else
                {
                    _ms_UserCenter_UserAttrs = _ms_UserCenter_UserAttrs.OrderBy(o => o.Uid);
                }
                ms_UserCenter_UserAttrs = _ms_UserCenter_UserAttrs.Skip(startRowIndex).Take(maximumRows).ToList();
                totalRecords = context.MS_UserCenter_UserAttr.Where<MS_UserCenter_UserAttr>(condition).Count();
            }
            return ms_UserCenter_UserAttrs;
        }

		/// <summary>
        /// 用户属性表-根据条件查询
        /// </summary>
        /// <param name="condition">条件</param>
        /// <param name="orderBy">排序条件</param>
        /// <param name="isDesc">是否倒序</param>
        /// <returns></returns>
        public List<MS_UserCenter_UserAttr> GetListByQuery(Expression<Func<MS_UserCenter_UserAttr, bool>> condition, string orderBy, bool isDesc)
        {
            List<MS_UserCenter_UserAttr> ms_UserCenter_UserAttrs = new List<MS_UserCenter_UserAttr>();
            using (MSDbContext context = new MSDbContext(WriteRead.Read))
            {
                var _ms_UserCenter_UserAttrs = context.MS_UserCenter_UserAttr.Where<MS_UserCenter_UserAttr>(condition);
                if (!string.IsNullOrEmpty(orderBy))
                {
                    try
                        {
                            string[] split = orderBy.Split(',');
                            for (int i = 0; i < split.Length; i++)
                            {
                                string[] param = split[i].Split(' ');
                                if(param.Length > 1)
                                {
                                    if (i == 0)
                                    {
                                        _ms_UserCenter_UserAttrs = _ms_UserCenter_UserAttrs.OrderBy(param[0], param[1] == "desc");
                                    }
                                    else
                                    {
                                        _ms_UserCenter_UserAttrs = _ms_UserCenter_UserAttrs.ThenBy(param[0], param[1] == "desc");
                                    }
                                }
                                else
                                {
                                    if (i == 0)
                                    {
                                        _ms_UserCenter_UserAttrs = _ms_UserCenter_UserAttrs.OrderBy(param[0], false);
                                    }
                                    else
                                    {
                                        _ms_UserCenter_UserAttrs = _ms_UserCenter_UserAttrs.ThenBy(param[0], false);
                                    }
                                }
                            }
                        }
                        catch
                        {
                            throw new Exception("OrderBy参数错误");
                        }
                }
                ms_UserCenter_UserAttrs = _ms_UserCenter_UserAttrs.ToList();
            }
            return ms_UserCenter_UserAttrs;
        }

		/// <summary>
        /// 用户属性表-根据条件查询
        /// </summary>
        /// <param name="condition">条件</param>
        /// <param name="orderBy">排序条件</param>
        /// <param name="isDesc">是否倒序</param>
        /// <returns></returns>
        public List<MS_UserCenter_UserAttr> GetListByQueryExpression(Expression<Func<MS_UserCenter_UserAttr, bool>> condition, Expression<Func<MS_UserCenter_UserAttr, object>> orderBy, bool isDesc)
        {
            List<MS_UserCenter_UserAttr> ms_UserCenter_UserAttrs = new List<MS_UserCenter_UserAttr>();
            using (MSDbContext context = new MSDbContext(WriteRead.Read))
            {
                var _ms_UserCenter_UserAttrs = context.MS_UserCenter_UserAttr.Where<MS_UserCenter_UserAttr>(condition);
                if (orderBy != null)
                {
                    _ms_UserCenter_UserAttrs = _ms_UserCenter_UserAttrs.OrderBy(EFHelper.GetPropertyName<MS_UserCenter_UserAttr>(orderBy), isDesc);
                }
                ms_UserCenter_UserAttrs = _ms_UserCenter_UserAttrs.ToList();
            }
            return ms_UserCenter_UserAttrs;
        }
    }
}
